package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.PositionService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.PositionMapper;
import cn.icare.cms.dao.mapper.UserPositionMenuMapper;
import cn.icare.cms.dao.po.Position;
import cn.icare.cms.dao.po.UserPositionMenu;
import cn.icare.cms.model.ro.AddPositionRO;
import cn.icare.cms.model.ro.EditPositionRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import cn.icare.cms.model.vo.rest.PositionVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PositionServiceImpl implements PositionService {
    @Resource
    private PositionMapper positionMapper;
    @Resource
    private UserPositionMenuMapper userPositionMenuMapper;

    @Autowired
    private  MenuServiceImpl menuService;

    private final Logger logger = LoggerFactory.getLogger(PositionServiceImpl.class);

    @Override
    public Long createOne(AddPositionRO userGroupVO) {
        Long result = -1L;
        try {
            Position userGroupDB = this.toDBByAddPositionRO(userGroupVO);
            if (this.positionMapper.insert(userGroupDB) > 0) {
                result = userGroupDB.getId();
            }
        } catch (Exception e) {
            this.logger.error("createOne 异常", e);
        }
        return result;
    }

    @Override
    public boolean updateOne(EditPositionRO ro) {
        boolean result = false;
        try {
            Position position=new Position();
            position.setId(ro.getId());
            position.setCreatetime(new Date());
            position.setName(ro.getName());
            position.setStatus(ro.getStatus());
            result = this.positionMapper.updateByPrimaryKey(position) > 0;
            if(result) {
                if(!menuService.updateUserPositionMenu(ro.getId().intValue(), ro.getMenuIds()))
                {
                    this.logger.error("updateOne 异常,更新岗位权限异常");
                }
            }
        } catch (Exception e) {
            this.logger.error("updateOne 异常", e);
        }
        return result;
    }

    @Override
    public boolean deleteOne(Integer id) {
        boolean result = false;
        try {
            result = this.positionMapper.deleteByPrimaryKey(id) > 0;
        } catch (Exception e) {
            this.logger.error("deleteOne 异常", e);
        }
        return result;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Position.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.positionMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public PositionVo getOne(Integer id) {
        PositionVo result = null;
        try {
          Position  position = this.positionMapper.selectByPrimaryKey(id);
          if(position!=null)
          {
              result=new PositionVo();
              result.setId(position.getId());
              result.setName(position.getName());
              result.setCreatetime(position.getCreatetime());
              result.setStatus(position.getStatus());
              List<Integer> userMenuList=this.menuService.getList(id);
              if(userMenuList!=null && userMenuList.size()>0)
              {
                  result.setMenuIds(userMenuList);
              }
          }

        } catch (Exception e) {
            this.logger.error("getOne 异常", e);
        }
        return result;
    }

    @Override
    public MsgDatasVO getList(String name, Integer pageNum, Integer pageSize) {
        MsgDatasVO result = new MsgDatasVO();
        if(pageSize==0)
        {
            //不分页，获取全部数据
            Example example = new Example(Position.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty(name)) {
                criteria.andLike("name", "%" + name + "%");
            }
            List<Position> userGroupDBList = this.positionMapper.selectByExample(example);
            result.setTotal(userGroupDBList.size());
            result.setDatas(userGroupDBList);
        }
        else {

            PageHelper.startPage(pageNum, pageSize);
            try {
                Example example = new Example(Position.class);
                Example.Criteria criteria = example.createCriteria();
                if (StringUtil.isNotEmpty(name)) {
                    criteria.andLike("name", "%" + name + "%");
                }
                List<Position> userGroupDBList = this.positionMapper.selectByExample(example);
                PageInfo pageInfo = new PageInfo(userGroupDBList);
                result.setTotal(pageInfo.getTotal());
                result.setDatas(userGroupDBList);
            } catch (Exception e) {
                this.logger.error("getList 异常", e);
            }
        }
        return result;
    }


    private  Position toDBByAddPositionRO(AddPositionRO ro)
    {
        Position result = null;
        try {
            if (ro != null) {
                result = new Position();
                result.setName(ro.getName());
                result.setStatus(ro.getStatus());
                result.setCreatetime(new Date());
            }
        } catch (Exception e) {
            this.logger.error("toDB 异常", e);
        }
        return result;
    }
}
